﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ASmile.Win.Controls
{
    public class EditDatePicker : EditText
    {
        public EditDatePicker()
        {

            MaxLength = 10;
            ShortcutsEnabled = false;
            //this.TextAlign = HorizontalAlignment.Center;
            base.Text = DateTime.Now.ToString(_FormatStr);

            //this.ResumeLayout(false);
            //this.PerformLayout();
        }

        bool _IsEnter = false;

        string _FormatStr = "yyyy-MM-dd";

        public string FormatStr { set { _FormatStr = value; } get { return _FormatStr; } }

        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                if (!DesignMode)
                {
                    base.Text = value;
                }
            }
        }

        bool InputCheck(char keyChar)
        {
            //char keyChar = (char)e.KeyCode;

            if ((keyChar >= '0' && keyChar <= '9'))
            //不输入输入除了数字之外的所有非法字符的判断
            {
                var r =
                    !(
                    (SelectionStart == 5 && keyChar > '1') || //月份第一位不能大于2
                    (SelectionStart == 6 && Text[5] > '0' && keyChar > '2') || //月份第二位
                    (SelectionStart == 8 && keyChar > '3') ||
                    (SelectionStart == 9 && Text[8] == '3' && keyChar > '1')
                    );

                if (r && SelectionStart >= 8)
                {
                    int year = Year;
                    int month = Month;
                    if (SelectionStart == 8 && month == 2 && keyChar > '2') return false;
                    if (SelectionStart == 9)
                    {
                        bool rn = year % 4 == 0 && year % 100 != 0;
                        if (!rn && month == 2 && Text[8] == '2' && keyChar == '0') return false;

                        if (Text[8] == '3')
                        {
                            var l = DateTime.DaysInMonth(year, month).ToString("00")[1];
                            if (keyChar > l) return false;
                        }
                    }
                }
                return r;
            }
            return false;
        }

        void SetTextChar(char keyData)
        {
            char[] txtChars = new char[10];
            for (int i = 0; i < txtChars.Length; i++)
            {
                txtChars[i] = Text[i];
                if (SelectionStart == i)
                {
                    txtChars[i] = keyData;
                }
            }
            if (SelectionStart == 5 && Text[5] > '1')
            {
                txtChars[5] = '0';
            }
            int selStart = SelectionStart;
            Text = new string(txtChars);
            Select(selStart, 1);
        }

        //void SetNextValue(int nextValue)
        //{
        //    if (SelectionStart < 2)
        //    {
        //        if (Month > 12)
        //        { 
                    
        //        }

        //        //时
        //        int h = int.Parse(Text.Substring(0, 2));
        //        h += nextValue;
        //        if (h > 23) h = 0;
        //        if (h < 0) h = 23;
        //        // $"{h:D2}{Text.Substring(2, 3)}";
        //        Text = string.Format("{0:D2}{1}", h, Text.Substring(2, 3));
        //        Select(0, 2);
        //    }
        //    else
        //    {
        //        //分
        //        int m = int.Parse(Text.Substring(3, 2));
        //        m += nextValue;
        //        if (m > 59) m = 0;
        //        if (m < 0) m = 59;
        //        //Text = $"{Text.Substring(0, 3)}{m:D2}";
        //        Text = string.Format("{0:D2}{1}", Text.Substring(0, 3), m);
        //        Select(3, 2);
        //    }
        //}

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            Select(0, 1);
            _IsEnter = true;
        }

        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (!_IsEnter) return;
            int selStart = base.SelectionStart;
            if (selStart == 4)
            {
                selStart = 5;
            }
            if (selStart == 7)
            {
                selStart = 8;
            }
            Select(selStart, 1);
            base.OnMouseClick(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                e.Handled = true;
            }

            if (e.KeyCode == Keys.Delete)
            {
                SetTextChar('0');
            }

            if (e.KeyCode == Keys.Back && SelectionStart > 0)
            {
                if (SelectionStart == 5) SelectionStart = 4;
                if (SelectionStart == 8) SelectionStart = 7;
                SelectionStart--;
                SetTextChar('0');
            }

            //if (e.KeyCode == Keys.Up)
            //{
            //    e.Handled = true;
            //    SetNextValue(1);
            //}
            //if (e.KeyCode == Keys.Down)
            //{
            //    e.Handled = true;
            //    SetNextValue(-1);
            //}

            base.OnKeyDown(e);
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (!InputCheck(e.KeyChar))
            {
                e.Handled = true;
            }
            else
            {
                SetTextChar(e.KeyChar);
            }

            base.OnKeyPress(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left && SelectionStart > 0)
            {
                if (SelectionStart == 5) SelectionStart = 4;
                SelectionStart--;
            }
            else if (SelectionStart == 4)
            {
                SelectionStart = 5;
            }
            else if (SelectionStart == 7)
            {
                SelectionStart = 8;
            }

            Select(SelectionStart, 1);

            base.OnKeyUp(e);
        }

        /// <summary>
        /// 获取日期的年份
        /// </summary>
        public int Year
        {
            get
            {
                return int.Parse(Text.Substring(0, 4));
            }
        }
        /// <summary>
        /// 获取日期的月份
        /// </summary>
        public int Month
        {
            get
            {
                return int.Parse(Text.Substring(5, 2));
            }
        }
        /// <summary>
        /// 获取日期的月份
        /// </summary>
        public int Day
        {
            get
            {
                return int.Parse(Text.Substring(8, 2));
            }
        }
        /// <summary>
        /// 获取时间DateTime
        /// </summary>
        public DateTime Value
        {
            get
            {
                return new DateTime(Year, Month, Day);
            }
            set
            {
                Text = value.ToString("yyyy-MM-dd");
            }
        }
        /// <summary>
        /// 实际值，等同于 Value
        /// </summary>
        public override object RealValue
        {
            get { return Value; }
            set
            {
                DateTime dt;
                if (DateTime.TryParse(value.ToString(), out dt))
                {
                    Value = dt;
                }
            }
        }
    }
}